import {
    BASE_URL
} from '../../../http/config';
Page({
    data: {
        img:BASE_URL,
        totalScore: 0,
        reportLevel: '',
        reportContent: '',
        suggestions: [],
        loading: true,
        formData: {
            userId: '',
            testId: ''
        },
        form: ''
    },

    onLoad(options) {
        // options 是一个对象，里面包含了跳转时携带的参数
        const testId = options.id;
        // 此时就获取到了传递过来的 id 值，为测试testId
        console.log('接收到的 id 为：', testId);
        this.generateReport(testId);
    },

    generateReport(testId) {
        // 获取全局用户信息
        const userInfo = wx.getStorageSync('userInfo');
        if (!userInfo || !userInfo.id) {
            console.error('用户信息未找到');
            return;
        }
        const userId = userInfo.id;

        this.setData({
            'formData.userId': userId,
            'formData.testId': testId
        });

        wx.request({
            url: `${BASE_URL}/psychologyTestResult/selSysUserId`,
            method: "GET",
            data: this.data.formData,
            success: (res) => {
                console.log('API响应:', res.data.data[0]);
                if (res.data && res.data.data) {
                    this.setData({
                        form: res.data.data[0]
                    });
                    // 安全获取分数
                    const score = this.getScoreSafely();
                    if (score !== null) {
                        this.setData({
                            totalScore: score
                        });
                        this.generateReportContent(score);
                    } else {
                        this.setData({
                            loading: false,
                            reportContent: '无法获取测试分数，请稍后再试'

                        });
                    }
                } else {
                    console.error('API返回数据格式错误', res);
                    this.setData({
                        loading: false,
                        reportContent: '服务器响应错误，请稍后再试'
                    });
                }
            }
        });
    },
   // 安全获取分数的辅助方法
getScoreSafely() {
    try {
        // 确保从正确的数据路径获取分数
        if (this.data.form && this.data.form.score !== undefined) {
            const score = parseInt(this.data.form.score);
            console.log('当前分数:', score);
            return isNaN(score) ? null : score;
        }
        return null;
    } catch (error) {
        console.error('获取分数时出错:', error);
        return null;
    }
},
    generateReportContent(score) {
        let report = {};
        if (score>=0 && score <= 30) {
            report = {
                level: "很不健康",
                content: "您的心理状态非常不健康，可能需要及时调整。",
                suggestions: [
                    "进行心理咨询",
                    "参加放松活动",
                    "保持规律作息"
                ]
            };
        } else if (score>=30 && score <= 60) {
            report = {
                level: "比较健康",
                content: "您近期可能有些压力，需要适当放松。",
                suggestions: [
                    "听音乐放松",
                    "进行户外运动",
                    "和朋友聊天"
                ]
            };
        } else if (score>=60&&score<= 90) {
            report = {
                level: "健康",
                content: "您的心理状态良好，继续保持。",
                suggestions: [
                    "保持良好的生活习惯",
                    "定期进行自我反思",
                    "培养兴趣爱好"
                ]
            };
        } else if (score>90&& score <=100) {
            report = {
                level: "非常健康",
                content: "您的心理状态极佳，继续保持积极心态。",
                suggestions: [
                    "分享快乐给他人",
                    "尝试新的挑战",
                    "保持学习的热情"
                ]
            };
        }

        this.setData({
            reportLevel: report.level,
            reportContent: report.content,
            suggestions: report.suggestions,
            loading: false,
            // 根据不同等级设置不同样式类
            reportLevelClass: report.level === "很不健康" ? "level-danger" :
                             report.level === "比较健康" ? "level-warning" :
                             report.level === "健康" ? "level-normal" : "level-success" 
        });
    }
});